<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"emoh.xyz","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="查看操作系统负载登上数据库服务器后，第一个就是通过系统命令确认下CPU、内存、I&#x2F;O是否异常，每个系统的命令不一样，常见的有top、topas、vmstat、iostat。 查看等待事件 第二步就是连到数据库查看活动的等待事件，这是监控、巡检、诊断数据库最基本的手段，通常81%的问题都可以通过等待事件初步定为原因，它是数据库运行情况最直接的体现，如下脚本是查看每个等待事件的个数、等待时长，并排除了">
<meta property="og:type" content="article">
<meta property="og:title" content="oracle数据库hang处理">
<meta property="og:url" content="http://emoh.xyz/2020/05/28/2020-05-28-oracle%E6%95%B0%E6%8D%AE%E5%BA%93hang%E5%A4%84%E7%90%86/index.html">
<meta property="og:site_name" content="Just do it!">
<meta property="og:description" content="查看操作系统负载登上数据库服务器后，第一个就是通过系统命令确认下CPU、内存、I&#x2F;O是否异常，每个系统的命令不一样，常见的有top、topas、vmstat、iostat。 查看等待事件 第二步就是连到数据库查看活动的等待事件，这是监控、巡检、诊断数据库最基本的手段，通常81%的问题都可以通过等待事件初步定为原因，它是数据库运行情况最直接的体现，如下脚本是查看每个等待事件的个数、等待时长，并排除了">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://emoh.xyz/2020/05/28/2020-05-28-oracle%E6%95%B0%E6%8D%AE%E5%BA%93hang%E5%A4%84%E7%90%86/imgclip.png">
<meta property="article:published_time" content="2020-05-28T02:50:12.000Z">
<meta property="article:modified_time" content="2020-05-28T03:35:00.305Z">
<meta property="article:author" content="Emoh">
<meta property="article:tag" content="hang">
<meta property="article:tag" content="故障">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://emoh.xyz/2020/05/28/2020-05-28-oracle%E6%95%B0%E6%8D%AE%E5%BA%93hang%E5%A4%84%E7%90%86/imgclip.png">

<link rel="canonical" href="http://emoh.xyz/2020/05/28/2020-05-28-oracle%E6%95%B0%E6%8D%AE%E5%BA%93hang%E5%A4%84%E7%90%86/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>oracle数据库hang处理 | Just do it!</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Just do it!</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://emoh.xyz/2020/05/28/2020-05-28-oracle%E6%95%B0%E6%8D%AE%E5%BA%93hang%E5%A4%84%E7%90%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.png">
      <meta itemprop="name" content="Emoh">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Just do it!">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          oracle数据库hang处理
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-05-28 10:50:12 / 修改时间：11:35:00" itemprop="dateCreated datePublished" datetime="2020-05-28T10:50:12+08:00">2020-05-28</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/oracle-%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86/" itemprop="url" rel="index"><span itemprop="name">oracle 问题处理</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="查看操作系统负载"><a href="#查看操作系统负载" class="headerlink" title="查看操作系统负载"></a>查看操作系统负载</h1><p>登上数据库服务器后，第一个就是通过系统命令确认下CPU、内存、I/O是否异常，每个系统的命令不一样，常见的有top、topas、vmstat、iostat。</p>
<h1 id="查看等待事件"><a href="#查看等待事件" class="headerlink" title="查看等待事件"></a>查看等待事件</h1><p> 第二步就是连到数据库查看活动的等待事件，这是监控、巡检、诊断数据库最基本的手段，通常81%的问题都可以通过等待事件初步定为原因，它是数据库运行情况最直接的体现，如下脚本是查看每个等待事件的个数、等待时长，并排除了一些常见的IDLE等待事件。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--墨天轮 wait_event</span></span><br><span class="line">col event for a45</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> INST_ID,</span><br><span class="line">       <span class="keyword">EVENT</span>,</span><br><span class="line">       <span class="keyword">SUM</span>(<span class="keyword">DECODE</span>(WAIT_TIME, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>)) <span class="string">"Prev"</span>,</span><br><span class="line">       <span class="keyword">SUM</span>(<span class="keyword">DECODE</span>(WAIT_TIME, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>)) <span class="string">"Curr"</span>,</span><br><span class="line">       <span class="keyword">COUNT</span>(*) <span class="string">"Tot"</span>,</span><br><span class="line">       <span class="keyword">SUM</span>(SECONDS_IN_WAIT) SECONDS_IN_WAIT</span><br><span class="line">  <span class="keyword">FROM</span> GV$SESSION_WAIT</span><br><span class="line"> <span class="keyword">WHERE</span> <span class="keyword">EVENT</span> <span class="keyword">NOT</span> <span class="keyword">IN</span> (<span class="string">'smon timer'</span>,</span><br><span class="line">                     <span class="string">'pmon timer'</span>,</span><br><span class="line">                     <span class="string">'rdbms ipc message'</span>,</span><br><span class="line">                     <span class="string">'SQL*Net message from client'</span>,</span><br><span class="line">                     <span class="string">'gcs remote message'</span>)</span><br><span class="line">   <span class="keyword">AND</span> <span class="keyword">EVENT</span> <span class="keyword">NOT</span> <span class="keyword">LIKE</span> <span class="string">'%idle%'</span></span><br><span class="line">   <span class="keyword">AND</span> <span class="keyword">EVENT</span> <span class="keyword">NOT</span> <span class="keyword">LIKE</span> <span class="string">'%Idle%'</span></span><br><span class="line">   <span class="keyword">AND</span> <span class="keyword">EVENT</span> <span class="keyword">NOT</span> <span class="keyword">LIKE</span> <span class="string">'%Streams AQ%'</span></span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span> INST_ID, <span class="keyword">EVENT</span></span><br><span class="line"> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="number">1</span>, <span class="number">5</span> <span class="keyword">DESC</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>

<h1 id="根据等待事件查会话"><a href="#根据等待事件查会话" class="headerlink" title="根据等待事件查会话"></a>根据等待事件查会话</h1><p>得到异常等待事件之后，我们就根据等待事件去查会话详情，也就是查看哪些会话执行哪些SQL在等待，另外还查出来用户名和机器名称，以及是否被阻塞。另外如下脚本可改写成根据用户查会话、根据SQL_ID查会话等等。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+rule */</span></span><br><span class="line"> <span class="keyword">SID</span>,</span><br><span class="line"> S.SERIAL<span class="comment">#,</span></span><br><span class="line"> SPID,</span><br><span class="line"> <span class="keyword">EVENT</span>,</span><br><span class="line"> SQL_ID,</span><br><span class="line"> SECONDS_IN_WAIT WS,</span><br><span class="line"> ROW_WAIT_OBJ<span class="comment"># OBJ,</span></span><br><span class="line"> S.USERNAME,</span><br><span class="line"> S.MACHINE,</span><br><span class="line"> BLOCKING_INSTANCE || <span class="string">'.'</span> || BLOCKING_SESSION B_SESS</span><br><span class="line">  <span class="keyword">FROM</span> V$<span class="keyword">SESSION</span> S, V$PROCESS P</span><br><span class="line"> <span class="keyword">WHERE</span> <span class="keyword">EVENT</span> = <span class="string">'&amp;event_name'</span></span><br><span class="line">   <span class="keyword">AND</span> S.PADDR = P.ADDR</span><br><span class="line"> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="number">6</span>;</span><br></pre></td></tr></table></figure>

<h1 id="查询某个会话详情"><a href="#查询某个会话详情" class="headerlink" title="查询某个会话详情"></a>查询某个会话详情</h1><p>得到会话列表之后，可以根据如下SQL查询某个会话的详细信息，如上次个执行的SQL_ID，登录时间等，该SQL也可改写成多个。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> S.SID,</span><br><span class="line">       S.SERIAL<span class="comment">#,</span></span><br><span class="line">       SPID,</span><br><span class="line">       <span class="keyword">EVENT</span>,</span><br><span class="line">       SQL_ID,</span><br><span class="line">       PREV_SQL_ID,</span><br><span class="line">       SECONDS_IN_WAIT  WS,</span><br><span class="line">       ROW_WAIT_OBJ<span class="comment">#    OBJ,</span></span><br><span class="line">       S.USERNAME,</span><br><span class="line">       S.MACHINE,</span><br><span class="line">       <span class="keyword">MODULE</span>,</span><br><span class="line">       BLOCKING_SESSION B_SESS,</span><br><span class="line">       LOGON_TIME</span><br><span class="line">  <span class="keyword">FROM</span> V$<span class="keyword">SESSION</span> S, V$PROCESS P</span><br><span class="line"> <span class="keyword">WHERE</span> <span class="keyword">SID</span> = <span class="string">'&amp;sid'</span></span><br><span class="line">   <span class="keyword">AND</span> S.PADDR = P.ADDR</span><br></pre></td></tr></table></figure>

<h1 id="查询对象信息"><a href="#查询对象信息" class="headerlink" title="查询对象信息"></a>查询对象信息</h1><p> 从前面两个SQL都可以看到会话等待的对象ID，可以通过如下SQL查询对象的详细信息。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">col OBJECT_NAME for a30</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> OWNER, OBJECT_NAME, SUBOBJECT_NAME, OBJECT_TYPE</span><br><span class="line">  <span class="keyword">FROM</span> DBA_OBJECTS</span><br><span class="line"> <span class="keyword">WHERE</span> OBJECT_ID = &amp;<span class="keyword">OID</span></span><br></pre></td></tr></table></figure>

<h1 id="查询会话阻塞情况"><a href="#查询会话阻塞情况" class="headerlink" title="查询会话阻塞情况"></a>查询会话阻塞情况</h1><p>通过如下SQL查询某个会话阻塞了多少个会话。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">COUNT</span>(*), BLOCKING_SESSION</span><br><span class="line">  <span class="keyword">FROM</span> V$<span class="keyword">SESSION</span></span><br><span class="line"> <span class="keyword">WHERE</span> BLOCKING_SESSION <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="literal">NULL</span></span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span> BLOCKING_SESSION;</span><br></pre></td></tr></table></figure>

<h1 id="查询数据库的锁"><a href="#查询数据库的锁" class="headerlink" title="查询数据库的锁"></a>查询数据库的锁</h1><p>通过如下SQL查询某个会话的锁，有哪些TM、TX锁，以及会话和锁关联查询的SQL，注意这里指定了ctime大于100秒，30%的情况是人为误操作锁表，导致应用SQL被阻塞，无法运行。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--墨天轮 lock</span></span><br><span class="line"><span class="keyword">set</span> linesize <span class="number">180</span></span><br><span class="line"><span class="keyword">col</span> username <span class="keyword">for</span> a15</span><br><span class="line"><span class="keyword">col</span> owner <span class="keyword">for</span> a15</span><br><span class="line"><span class="keyword">col</span> OBJECT_NAME <span class="keyword">for</span> a30</span><br><span class="line"><span class="keyword">col</span> SPID <span class="keyword">for</span> a10</span><br><span class="line"><span class="comment">--查询某个会话的锁</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+rule*/</span></span><br><span class="line"> SESSION_ID, OBJECT_ID, ORACLE_USERNAME, OS_USER_NAME, PROCESS, LOCKED_MODE</span><br><span class="line">  <span class="keyword">FROM</span> GV$LOCKED_OBJECT</span><br><span class="line"> <span class="keyword">WHERE</span> SESSION_ID = &amp;<span class="keyword">SID</span>;</span><br><span class="line"><span class="comment">--查询TM、TX锁</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+rule*/</span></span><br><span class="line"> *</span><br><span class="line">  <span class="keyword">FROM</span> V$<span class="keyword">LOCK</span></span><br><span class="line"> <span class="keyword">WHERE</span> CTIME &gt; <span class="number">100</span></span><br><span class="line">   <span class="keyword">AND</span> <span class="keyword">TYPE</span> <span class="keyword">IN</span> (<span class="string">'TX'</span>, <span class="string">'TM'</span>)</span><br><span class="line"> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="number">3</span>, <span class="number">9</span>;</span><br><span class="line"><span class="comment">--查询数据库中的锁</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+rule*/</span></span><br><span class="line"> S.SID,</span><br><span class="line"> P.SPID,</span><br><span class="line"> L.TYPE,</span><br><span class="line"> <span class="keyword">ROUND</span>(<span class="keyword">MAX</span>(L.CTIME) / <span class="number">60</span>, <span class="number">0</span>) LOCK_MIN,</span><br><span class="line"> S.SQL_ID,</span><br><span class="line"> S.USERNAME,</span><br><span class="line"> B.OWNER,</span><br><span class="line"> B.OBJECT_TYPE,</span><br><span class="line"> B.OBJECT_NAME</span><br><span class="line">  <span class="keyword">FROM</span> V$<span class="keyword">SESSION</span> S, V$PROCESS P, V$<span class="keyword">LOCK</span> L, V$LOCKED_OBJECT O, DBA_OBJECTS B</span><br><span class="line"> <span class="keyword">WHERE</span> O.SESSION_ID = S.SID</span><br><span class="line">   <span class="keyword">AND</span> S.SID = L.SID</span><br><span class="line">   <span class="keyword">AND</span> O.OBJECT_ID = B.OBJECT_ID</span><br><span class="line">   <span class="keyword">AND</span> S.PADDR = P.ADDR</span><br><span class="line">   <span class="keyword">AND</span> L.CTIME &gt; <span class="number">100</span></span><br><span class="line">   <span class="keyword">AND</span> L.TYPE <span class="keyword">IN</span> (<span class="string">'TX'</span>, <span class="string">'TM'</span>, <span class="string">'FB'</span>)</span><br><span class="line"> <span class="keyword">GROUP</span> <span class="keyword">BY</span> S.SID,</span><br><span class="line">          P.SPID,</span><br><span class="line">          L.TYPE,</span><br><span class="line">          S.SQL_ID,</span><br><span class="line">          S.USERNAME,</span><br><span class="line">          B.OWNER,</span><br><span class="line">          B.OBJECT_TYPE,</span><br><span class="line">          B.OBJECT_NAME</span><br><span class="line"> <span class="keyword">ORDER</span> <span class="keyword">BY</span> <span class="number">9</span>, <span class="number">1</span>, <span class="number">3</span></span><br></pre></td></tr></table></figure>

<h1 id="保留现场证据"><a href="#保留现场证据" class="headerlink" title="保留现场证据"></a>保留现场证据</h1><p>有的问题可能需要分析很长时间，或者是需要外部人员协助分析，那么保留现场证据就非常重要了，下面脚本是systemstate dump和hanganalyze步骤，如果有sqlplus无法登陆的情况，可以加-prelim参数。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--systemstate dump</span></span><br><span class="line">sqlplus -prelim / as sysdba</span><br><span class="line">oradebug setmypid</span><br><span class="line">oradebug unlimit;</span><br><span class="line">oradebug dump systemstate 266;</span><br><span class="line"><span class="comment">--wait for 1 min</span></span><br><span class="line">oradebug dump systemstate 266;</span><br><span class="line"><span class="comment">--wait for 1 min</span></span><br><span class="line">oradebug dump systemstate 266;</span><br><span class="line">oradebug tracefile_name;</span><br><span class="line"></span><br><span class="line"><span class="comment">--hanganalyze</span></span><br><span class="line">oradebug setmypid</span><br><span class="line">oradebug unlimit;</span><br><span class="line">oradebug dump hanganalyze 3</span><br><span class="line"><span class="comment">--wait for 1 min</span></span><br><span class="line">oradebug dump hanganalyze 3</span><br><span class="line"><span class="comment">--wait for 1 min</span></span><br><span class="line">oradebug dump hanganalyze 3</span><br><span class="line">oradebug tracefile_name</span><br></pre></td></tr></table></figure>

<h1 id="杀会话"><a href="#杀会话" class="headerlink" title="杀会话"></a>杀会话</h1><p>通常情况下，初步定为问题后为了快速恢复业务，需要去杀掉某些会话，特别是批量杀会话，有时还会直接kill所有LOCAL=NO的进程，再杀会话时一定要检查确认，更不能在别的节点或者别的服务器上执行。</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">--墨天轮 kill_sess</span></span><br><span class="line"><span class="keyword">set</span> line <span class="number">199</span></span><br><span class="line"><span class="keyword">col</span> <span class="keyword">event</span> <span class="keyword">format</span> a35</span><br><span class="line"><span class="comment">--杀某个SID会话</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+ rule */</span> <span class="keyword">sid</span>, s.serial<span class="comment">#, 'kill -9 '||spid, event, blocking_session b_sess </span></span><br><span class="line"><span class="keyword">FROM</span> v$<span class="keyword">session</span> s, v$process p <span class="keyword">WHERE</span> <span class="keyword">sid</span>=<span class="string">'&amp;sid'</span> <span class="keyword">AND</span> s.paddr = p.addr <span class="keyword">order</span> <span class="keyword">by</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">--根据SQL_ID杀会话</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+ rule */</span> <span class="keyword">sid</span>, s.serial<span class="comment">#, 'kill -9 '||spid, event, blocking_session b_sess </span></span><br><span class="line"><span class="keyword">FROM</span> v$<span class="keyword">session</span> s, v$process p <span class="keyword">WHERE</span> sql_id=<span class="string">'&amp;sql_id'</span> <span class="keyword">AND</span> s.paddr = p.addr <span class="keyword">order</span> <span class="keyword">by</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">--根据等待事件杀会话</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+ rule */</span> <span class="keyword">sid</span>, s.serial<span class="comment">#, 'kill -9 '||spid, event, blocking_session b_sess </span></span><br><span class="line"><span class="keyword">FROM</span> v$<span class="keyword">session</span> s, v$process p <span class="keyword">WHERE</span> <span class="keyword">event</span>=<span class="string">'&amp;event'</span> <span class="keyword">AND</span> s.paddr = p.addr <span class="keyword">order</span> <span class="keyword">by</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">--根据用户杀会话</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="comment">/*+ rule */</span> <span class="keyword">sid</span>, s.serial<span class="comment">#, 'kill -9 '||spid, event, blocking_session b_sess </span></span><br><span class="line"><span class="keyword">FROM</span> v$<span class="keyword">session</span> s, v$process p <span class="keyword">WHERE</span> username=<span class="string">'&amp;username'</span> <span class="keyword">AND</span> s.paddr = p.addr <span class="keyword">order</span> <span class="keyword">by</span> <span class="number">1</span>;</span><br><span class="line"><span class="comment">--kill所有LOCAL=NO进程</span></span><br><span class="line">ps -ef|grep LOCAL=NO|grep $ORACLE_SID|grep -v grep|awk '&#123;print $2&#125;' |xargs kill -9</span><br></pre></td></tr></table></figure>

<h1 id="重启大法"><a href="#重启大法" class="headerlink" title="重启大法"></a>重启大法</h1><p>如需要修改静态参数、内存等问题，需要重启数据库，（不要觉得重启很LOW，在很多情况下为了快速恢复业务经常使用这个从网吧里传出来的绝招），记住千万不要在这个时候死磕问题原因、当作课题研究，我们的首要任务是恢复业务。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">tail -f alert_.log</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">system</span> checkpoint;</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">system</span> <span class="keyword">switch</span> <span class="keyword">logfile</span>;</span><br><span class="line">shutdown immediate;</span><br><span class="line"></span><br><span class="line">startup</span><br></pre></td></tr></table></figure>

<h1 id="SECURECRT按钮小技巧"><a href="#SECURECRT按钮小技巧" class="headerlink" title="SECURECRT按钮小技巧"></a>SECURECRT按钮小技巧</h1><p>另外介绍一个小技巧，就是把常用的脚本整理到SecureCRT的Button Bar中，只需要点一下设置好的button，就相当于直接执行相应的SQL语句，这样就不用每次粘贴复制执行，或者是把脚本上传到每个服务器上。不过不要设置DDL等操作性的button，以免误点。</p>
<p><img src="imgclip.png" alt="" title="imgclip.png"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/hang/" rel="tag"># hang</a>
              <a href="/tags/%E6%95%85%E9%9A%9C/" rel="tag"># 故障</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/27/2019-07-24-%E8%AE%BE%E7%BD%AEoracle%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%86%E7%A0%81%E6%B0%B8%E4%B8%8D%E8%BF%87%E6%9C%9F(ORA-28002)/" rel="prev" title="设置oracle数据库密码永不过期(ORA-28002)">
      <i class="fa fa-chevron-left"></i> 设置oracle数据库密码永不过期(ORA-28002)
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/28/2020-05-28-oracle%E9%97%AA%E5%9B%9E%E7%89%B9%E6%80%A7/" rel="next" title="oracle闪回特性">
      oracle闪回特性 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#查看操作系统负载"><span class="nav-number">1.</span> <span class="nav-text">查看操作系统负载</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查看等待事件"><span class="nav-number">2.</span> <span class="nav-text">查看等待事件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#根据等待事件查会话"><span class="nav-number">3.</span> <span class="nav-text">根据等待事件查会话</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查询某个会话详情"><span class="nav-number">4.</span> <span class="nav-text">查询某个会话详情</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查询对象信息"><span class="nav-number">5.</span> <span class="nav-text">查询对象信息</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查询会话阻塞情况"><span class="nav-number">6.</span> <span class="nav-text">查询会话阻塞情况</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#查询数据库的锁"><span class="nav-number">7.</span> <span class="nav-text">查询数据库的锁</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#保留现场证据"><span class="nav-number">8.</span> <span class="nav-text">保留现场证据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#杀会话"><span class="nav-number">9.</span> <span class="nav-text">杀会话</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#重启大法"><span class="nav-number">10.</span> <span class="nav-text">重启大法</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#SECURECRT按钮小技巧"><span class="nav-number">11.</span> <span class="nav-text">SECURECRT按钮小技巧</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Emoh"
      src="/images/touxiang.png">
  <p class="site-author-name" itemprop="name">Emoh</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">16</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Emoh</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
